<template>
    <el-dialog title="调账" :visible.sync="dialogAdjustForm" width="860px">
        <el-form :inline="true" ref="form" :model="form" :rules="rules" class="add-form" label-width="100px">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="结算单位:" prop="accountUnit">
                        {{form.accountUnit}}
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="调账月度:" prop="accountMonth">
                        {{form.accountMonth}}
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="作业港口:" prop="showPortCode">
                        <select-port :name="portName" v-model="showPortCode"></select-port>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="作业类型:" prop="planType">
                        <el-select v-model="form.planType" placeholder="请选择">
                            <el-option v-for="[key, val] in _dictAllJobStatus" :key="key" :label="val"
                                       :value="key"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="调账类型:" prop="accountChangeType" >
                        <el-radio v-model="choiceType" :label="1"  @change.native="changeAdjustType">一个单位调账</el-radio>
                        <el-radio v-model="choiceType" :label="2"  @change.native="changeAdjustType">两个单位互调</el-radio>
                    </el-form-item>
                </el-col>
                <el-col :span="12" v-if="isTwoAdjust">
                    <el-form-item label="对方单位:" prop="accountSideUnit">
                        <search-custom @selectName="v=>form.accountSideUnit=v" :isSpecialForm="true"></search-custom>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="调整方式:" prop="accountChangeWay">
                        <el-select v-model="form.accountChangeWay" placeholder="请选择">
                            <el-option v-for="[key, val] in _dictAccountChangeWay" :key="key" :label="val"
                                       :value="key"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12" v-if="isTwoAdjust">
                    <el-form-item label="对方调整:" prop="sideChangeWay">
                        <span :style="form.accountChangeWay == 'ADD' ? 'color:red' : 'color:green'">{{form.accountChangeWay == 'ADD' ? '扣减' : '增补'}}</span>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="调整箱量:" prop="feeNum">
                        <el-input v-model="form.feeNum" @change="countMoney"></el-input>
                    </el-form-item>
                    <span>TEU</span>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="调整费率:" prop="feePrice">
                        <el-input v-model="form.feePrice" @change="countMoney"></el-input>
                    </el-form-item>
                    <span>元</span>
                </el-col>
                <el-col :span="12" v-if="isTwoAdjust">
                    <el-form-item label="对方调整费率:" prop="feeSidePrice">
                        <el-input v-model="form.feeSidePrice" @change="countMoney"></el-input>
                    </el-form-item>
                    <span>元</span>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="调整金额:" prop="money">
                        {{form.money}}{{form.money != "" ? '元':''}}
                    </el-form-item>
                </el-col>
                <el-col :span="12" v-if="isTwoAdjust">
                    <el-form-item label="对方调整金额:" prop="sideMoney">
                        {{form.sideMoney}}{{form.sideMoney != "" ? '元':''}}
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label="调账备注:" prop="remark">
                        <el-input type="textarea" :rows="3" maxlength="255" style="width: 580px;" placeholder="请输入内容" v-model="form.remark"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="调整人:" prop="userName">
                        {{userName}}
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="调账时间:" prop="adjustTime">
                        {{adjustTime}}
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogAdjustForm = false">取 消</el-button>
            <el-button type="primary" @click="handleSubmit" :loading="$store.getters.loadingBtn">保 存</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import { PUBLIC_NUMBER, PUBLIC_NUM_CODE, REG_LETTER_NUM_POINT, PUBLIC_INT_NULL, REG_NUMBER } from "@/utils/Reg";
    import { maxLength, sortLength } from "@/utils/rules";
    import { addOrChangeAccount } from "@/api/balance/balance";
    import { getLocalTime } from "@/utils/dateFormat";
    import selectPort from '@/components/select/selectPort'
    import searchCustom from '@/components/autocomplete/autoSearchCustom';

    export default {
        name: "adjustAccountForm",
        components: {selectPort,  searchCustom},
        data() {
            return {
                form: this.clearForm(),
                rules: {
                    planType: [{required: true, message: "请选择作业类型", trigger: "change"}],
                    accountChangeWay : [{required: true, message: "请选择调整方式", trigger: "change"}],
                    feeNum : [
                        {required: true, message: "请填写调整箱量", trigger: "change"},
                        {pattern: REG_NUMBER, message: '只能输入整数', trigger: "change"},
                    ],
                    feePrice : [
                        {required: true,message: "请填写调整费率", trigger: "change"},
                        {pattern: PUBLIC_NUMBER, message: '只能输入整数或者小数', trigger: "change"},
                    ],
                    feeSidePrice : [
                        {pattern: PUBLIC_NUMBER, message: '只能输入整数或者小数', trigger: "change"},
                    ]
                },
                sideChangeWay : '',
                choiceType : 1,
                isTwoAdjust:false,
                portName : '',
                userName :'',
                showPortCode : '',
                adjustTime:getLocalTime(new Date(), "YYYY-MM-DD hh:mm:ss"),
                dialogAdjustForm: false
            };
        },
        methods: {
            clearForm() {
                return {
                    accountBillId:"",
                    accountUnit : "",
                    accountMonth : "",
                    planType : "",
                    accountChangeType : "",
                    accountSideUnit : "",
                    accountChangeWay : "ADD",
                    feeNum : "",
                    feePrice : "",
                    feeSidePrice : "",
                    money : "",
                    sideMoney : "",
                    remark : ""
                };
            },
            // 保存
            handleSubmit() {
                this.$refs.form.validate(valid => {
                    if (valid) {
                        if (this.choiceType == 2) {
                            if (this.form.accountSideUnit == "" || this.form.accountSideUnit == null) {
                                this.$message({
                                    message: "请填写对方单位",
                                    type: "error"
                                });
                                return;
                            }
                            if (this.form.feeSidePrice == "" || this.form.feeSidePrice == null) {
                                this.$message({
                                    message: "请填写对方调整费率",
                                    type: "error"
                                });
                                return;
                            }
                            this.form.accountChangeType = "TWO"
                            if (this.form.accountChangeWay == "ADD"){
                                this.$set(this.form, 'accountSideChangeWay', "REDUCE")
                            } else {
                                this.$set(this.form, 'accountSideChangeWay', "ADD")
                            }
                        } else {
                            this.form.accountChangeType = "ONE"
                        }
                        if (this.form.sideMoney == "") {
                            this.form.sideMoney = 0
                        }
                        if (this.form.feeNum != "") {
                            this.form.feeNum = Number(this.form.feeNum)
                        }
                        this.handleToAdd();
                    } else {
                        this.$message({
                            message: "请检查数据",
                            type: "error"
                        });
                    }
                });
            },
            // 新增
            handleToAdd() {
                if (this.showPortCode != ""){
                    this.$set(this.form, 'portCode', this.showPortCode)
                }
                addOrChangeAccount(this.form).then(res => {
                    this.$message({
                        message: "新增成功!",
                        type: "success"
                    });
                    this.dialogAdjustForm = false;
                    this.$emit("load");
                });
            },
            changeAdjustType(){
                if (this.choiceType == 1){
                    this.isTwoAdjust = false
                } else {
                    this.isTwoAdjust = true
                }
            },
            countMoney(){
                if (this.form.feeNum != null && this.form.feeNum != ""){
                    if (this.form.feePrice != ""){
                        this.form.money = (Number(this.form.feeNum)*Number(this.form.feePrice)).toFixed(2)
                    }
                    if (this.form.feeSidePrice != ""){
                        this.form.sideMoney = (Number(this.form.feeNum)*Number(this.form.feeSidePrice)).toFixed(2)
                    }
                } else {
                    this.form.money = ""
                    this.form.sideMoney = ""
                }
            }
        },
        watch: {
            dialogAdjustForm(bool) {
                if (!bool) {
                    this.$refs.form.resetFields();
                    this.form = this.clearForm();
                }
            }
        }
    };
</script>

<style scoped>
</style>
